pathlib: use PID liveness as primary stale-lock signal - #14937
Open
jawauntb wants to merge 2 commits into
Open
Conversation
`ensure_deletable` previously treated a `.lock` file as dead purely by mtime age, with LOCK_TIMEOUT = 3 days. The PID inside the lock was never consulted. Two failure modes followed: - A crashed session's scratch survives for three days before it can be reaped, even though the process is obviously gone. - A long-running session (some suites do run for hours) whose lock happens to be older than three days can have its own scratch reaped mid-run. This change reads the PID out of the lock file and probes it with `os.kill(pid, 0)` on POSIX and `OpenProcess` on Windows. If the PID is provably not running, the lock is unlinked and the directory is reported deletable regardless of clock. If the PID is alive, the directory is not deletable regardless of clock. The mtime-based check remains as a fallback for locks whose contents cannot be parsed (empty, corrupted, or partially written). Refs pytest-dev#14935.
jawauntb
force-pushed
the
jb/tmpdir-pid-liveness
branch
from
August 25, 2026 23:07
c48f281 to
8c91612
Compare
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #14935 (part 2 of 3).
What
ensure_deletablein_pytest/pathlib.pynow reads the PID stored in asession's
.lockfile and probes it for liveness (os.kill(pid, 0)onPOSIX,
OpenProcesson Windows) as its primary signal. The historicalLOCK_TIMEOUT = 3 daysmtime check is retained as a fallback for lockswhose contents cannot be parsed (empty, corrupted, partially written).
Why
The current implementation treats a
.lockas dead purely by mtime age:Two failure modes fall out of that:
isn't reclaimable until three days after its lock was last touched, even
though the process is obviously gone.
.lockmtime crosses the three-day threshold can have its own scratch removed
by a concurrent cleanup.
Both are fixed by the same one-line diagnosis: pytest already writes the
PID into the lock (
create_cleanup_lockinpathlib.py); it just neverreads it back.
Behavior matrix
Windows
Windows liveness uses
OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, …)plus
GetExitCodeProcess.ERROR_INVALID_PARAMETER(87) is the canonical"no such process" return; any other failure is treated conservatively as
"assume alive" so unfamiliar edge cases fall through to the mtime fallback
rather than aggressively deleting.
PID recycling
If a lock's PID has been recycled to a different process,
_pid_alivereturns True and cleanup waits. That's conservative but safe: the mtime
fallback still takes over after
LOCK_TIMEOUT. This does not regress anyexisting behavior.
Tests
test_cleanup_lockedupdated: with the current process's PID in thelock,
ensure_deletablereturns False regardless of mtime threshold —which is the point of the change.
test_cleanup_dead_pid_deletable_regardless_of_mtime— asserts a locknaming a nonexistent PID is deletable immediately, without waiting for
LOCK_TIMEOUT.test_cleanup_unreadable_lock_falls_back_to_mtime— asserts the mtimepath still applies when the lock's contents can't be parsed.
Local runs:
test_pathlib.py+test_tmpdir.py+test_pytester.py→223 passed, 3 skipped, 1 xfailed.
Scope
Independent of #14936 (the
.originsidecar). Either can land first.The third planned PR (per-rootdir retention layout) is not in this change.
Changelog
changelog/14935.improvement.rst— same tracking issue as the other twoPRs; towncrier will render them together.
🤖 Generated with Claude Code
https://claude.ai/code/session_017o9EnCzHyTdYKdcXshMyMZ